// pages/chuzuwuping/chuzuwuping.js
const app = getApp();
Component({
  properties: {
    fileList: {
      type: Array
    },
    prevent: {
      type: Boolean,
      value: false
    }
  },
  data: {
    fit:0,
    fileList: [],
    List:[],
    fileList1:[],
    imgList:[],
    n:0,
    flag:true,
    openid:''
  },
  ready() { this.setData({
    openid:wx.getStorageSync('openid')
  })},
  onLoad: function (options) {
    this.setData({
      openid:wx.getStorageSync('openid')
    })
  },
  methods: {
    // 点击加号进入手机相册，并进行图片选择
    _addImg() {
      let _this = this;
      var a=_this.data.n;
      
      if(a<2){
        _this.data.n++;
      // 此方法为微信小程序自带api 详情访问https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html
      wx.chooseImage({
        count: 5,
        success(res) {
          //此处会返回图片暂存路径和文件大小
          const data = res.tempFiles;
          console.log(res)
          console.log(data)
          _this.setFile(data)
          _this.setData({
            fileList1:_this.data.fileList1.concat(res.tempFilePaths)
          })
          console.log(_this.data.fileList1)
        }
      })
    }else{
      _this.data.n++;
      wx.chooseImage({
        count: 5,
        success(res) {
          //此处会返回图片暂存路径和文件大小
          const data = res.tempFiles;
          _this.setFile(data)
          _this.setData({
            fileList1:_this.data.fileList1.concat(res.tempFilePaths)
          })
          console.log(_this.data.fileList1)
        }
      })
      _this.setData({
        flag:false
      })
      
    }
    },
    setFile (data) {
      // 将wx.chooseImage返回的数据进行扩展
      data.map((item, index) => {
        // 通过路径截取文件后缀名
        const fileFormat = item.path.substring(item.path.lastIndexOf(".") + 1, item.path.length);
        // wx.getFileSystemManager()小程序文件管理器api，可以将通过文件路径将其转换成64编码
        const fileManager = wx.getFileSystemManager();
        const base64 = fileManager.readFileSync(item.path, 'base64');
        item.fileContent = base64;
        item.fileSize = item.size;
        // 通过时间获取随机13位随机数并且拼接文件后缀进行文件命名
        item.fileName = this.getFileName(13) + '.' + fileFormat;
        // 此处操作是用来进行选中图片显示的，只有这样拼接才能显示base64编码的路径
        item.path = `data:image/${fileFormat};base64,${base64}`;;
      })
      this.setData({ 
        fileList: this.data.fileList.concat(data)
      });
      // 此处操作是用来将获取到的文件数据传递给父组件进行文件上传
      this.triggerEvent('imageChange', this.data.fileList)
    },
    // 随机生成文件名
    getFileName (m) {
      m = m > 13 ? 13 : m;
      var num = new Date().getTime();
      return num.toString().substring(13 - m);
    },
    // 点击进行图片删除
    _onDelTab(e) {
      // 获取图片索引
      let idx = e.currentTarget.dataset.idx;
      let delFile = this.data.fileList[idx];
      console.log(delFile);
      this.data.fileList.splice(idx, 1);
      this.setData({
        fileList: this.data.fileList
      })
      this.triggerEvent('imageDel', delFile);
    },

    onShow: function () {

    },
    
    formSubmit: function (e) {
      
      console.log(e.detail.value)
      if(e.detail.value.age2>2){
        this.setData({
          fit:0
        })
      }else if(e.detail.value.age2>6){
        this.setData({
          fit:1
        })
      }else if(e.detail.value.age2>10){
        this.setData({
          fit:2
        })
      }else if(e.detail.value.age2>15){
        this.setData({
          fit:3
        })
      }


      this.setData({
        List:e.detail.value
      })
      console.log(this.data.fileList1)
      console.log(this.data.openid)
      for(var i=0;i<this.data.n;i++){
      wx.uploadFile({
        url: 'http://localhost:80/img/upload',
        filePath: this.data.fileList1[i],
        name: 'mFile',
        header: {
          "content-type": "multipart/form-data"
        },
        success: (res) => {
          const data = res.data
          res.data="/tupian/后台/儿童用品img/14/"+res.data
          this.setData({
            imgList:this.data.imgList.concat(res.data)
          })
          console.log(this.data.imgList)
         
          console.log(this.data.imgList[0])
        }
      })
    }
    for(var i=0;i<this.data.n;i++){
      wx.uploadFile({
        url: 'http://localhost:80/img/upload1',
        filePath: this.data.fileList1[i],
        name: 'mFile',
        header: {
          "content-type": "multipart/form-data"
        },
        success: (res) => {
        
        }
      })
    }
    setTimeout((function callback() {
      this.setData({bodySubmitFlag:false})
  }).bind(this), 2000);
    wx.request({
      url: 'http://localhost:80/img/insert',
      method:"POST",
      data:{
        title:e.detail.value.name,
        category:1,
        fit:this.data.fit,
        rent:e.detail.value.feiyong1,
        freight:e.detail.value.feiyong2,
        deposit:e.detail.value.feiyong3,
        addres:e.detail.value.address,
        phone:e.detail.value.phone,
        describes:e.detail.value.miaoshu,
        imgurl1:this.data.imgList[0],
        imgurl2:this.data.imgList[1],
        imgurl3:this.data.imgList[2],
        leixin1:e.detail.value.leixin,
        leixin2:e.detail.value.leixin1,
        leixin3:e.detail.value.leixin2,
        num:3,
        state:"状态",
        userid:this.data.openid
      },
      header:{
        'content-type': 'application/x-www-form-urlencoded'
    },
    success:res=>{
      wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000//持续的时间
            })
            this.onShow();
      }
    })
      



      // wx.request({
      //     wx.showToast({
      //       title: '提交成功',
      //       icon: 'success',
      //       duration: 2000//持续的时间
      //     })
      //   }
      // })
    }
  }
})
